/**
 * Created by 123 on 2017/12/8.
 * 签退
 */
import {querySignInfo, signIn} from '../services/signOut';
import {Toast} from 'antd-mobile'
import {Actions} from 'react-native-router-flux'
import fetch from "dva/fetch";

function showToast(msg) {
  Toast.fail(msg, 1)
}
export default {
  namespace: 'signOut',

  state: {
    taskName: '',//任务名称
    taskId: '',//任务id
    taskNum: '',//任务编号
    projectName: '',//工程名称
    projectNum: '',//工程编号
    signName: '',//签退人
    signTime: '',//签退时间
    signPlace: '',//签退地点
    taskPhotoList: [],//现场照片数组{photoPath:照片路径}
    taskSignNameList: [],//签名数组{signNamePath:照片路径}
    taskUserList: [],//人员数组{value:人员id,label:人员姓名}
    currentUserList: [],//选择签退的人员{id:人员id}
    dialogText:''
  },

  subscriptions: {},

  effects: {
    //获取签退信息
    *querySignInfo({payload}, {call, put, select}){
      const {taskIdUnique:taskId}=yield select(state => state.taskReceive);
      const {res, error}=yield call(querySignInfo, {id: taskId})
      if (error) {
        yield put({type: 'querySignInfoFail', payload: error})
      } else {
        // console.log('querySignInfo res')
        // console.log(res)
        if (res.code == 1) {
          yield put({type: 'querySignInfoSuccess', payload: res.data})
        } else {
            yield put({type:'thisDialog/thisInit'});
            yield put({type:'dialogShow',payload:res.message});
          yield put({type: 'querySignInfoFail', payload: res.message})

        }

      }
    },
    //签退
    *signIn({payload}, {call, put, select}){
      const {taskId, signTime, signPlace, currentUserList}=yield select(state => state.signOut);
      const {singOutImgs:photoList}=yield select(state => state.photo);
      const {signOutSignature:signList}=yield select(state => state.sign);
      const {projectId}=yield select(state => state.projectsList);

      if (signPlace == '') {
          yield put({type:'thisDialog/thisInit'});
          yield put({type:'dialogShow',payload:'请定位签退地点'});
      }
      if (currentUserList.length == 0) {
          yield put({type:'thisDialog/thisInit'});
          yield put({type:'dialogShow',payload:'请选择签到人'});
      }
      if (photoList.length == 0) {
          yield put({type:'thisDialog/thisInit'});
          yield put({type:'dialogShow',payload:'请拍摄现场照片'});
      }
      if (signList.length == 0) {
          yield put({type:'thisDialog/thisInit'});
          yield put({type:'dialogShow',payload:'请签字'});
      }

      let param = {
        id: taskId,
        signOutPlace: signPlace,
        taskPhotoList: photoList,
        taskSignNameList: signList,
        taskUserList: currentUserList
      };
      // Actions.push('taskOperations');
      const {res, error}=yield call(signIn, param);
      if (error) {
        yield put({type: 'signInFail', payload: error})
      } else {
        if(res.code==1){
            yield put({type:'taskCenter/getTableList'});
          Actions.pop()
          yield put({type: 'signInSuccess', payload: res});
          yield put({type: 'tasksList/queryTasks'});
        }else {
          yield put({type: 'signInFail', payload: res.message})
            yield put({type:'thisDialog/thisInit'});
            yield put({type:'dialogShow',payload:res.message});
        }

      }
    },
    *getClean({payload},{select,call,put}){
      yield put({type:'getThisClean'})
      yield put({type:'photo/cleanPhotoImgs'})
      yield put({type:'sign/cleanSignImgs'})
    },
    *addPictures({payload}, {put, call, select}){
      let data = new FormData()
      data.append('file', {
        uri: payload.uri,
        type: 'image/jpeg',
        name: payload.name,
      });

      try {
        const res = yield fetch(GLOBAL.INT.imageUpload, {
          method: 'post',
          headers: {
            'Content-Type': 'multipart/form-data',
          },
          body: data,
        });

        const json = yield res.json();
        console.log(res,'res.....')
        const url = json.data.fileurl;

        if (url !== null) {
          yield put({type: 'photo/saveSuccess', payload: {url: url, type: payload.type}})
        }

      } catch (e) {
        console.error(e);
      }

    },
  },

  reducers: {
    //获取签退信息成功
    querySignInfoSuccess(state, {payload}){
      const {task, taskUserList}=payload;

      let userList = [];
      taskUserList.map((val, index) => {
        userList.push({value: val.userId, label: val.userName})
      });

      return {
        ...state,
        taskName: task.name,//任务名称
        taskId: task.id,//任务id
        taskNum: task.taskNum,//任务编号
        projectName: task.prjName,//工程名称
        projectNum: task.prjnum,//工程编号
        taskUserList: userList,//签退人
      }
    },
    //获取签退信息失败
    querySignInfoFail(state, {payload}){

      return {...state}
    },
    //签退成功
    signInSuccess(state, {payload}){
      return {...state}
    },
    //签退失败
    signInFail(state, {payload}){
      return {...state}
    },
    //设置签退地点
    setSignPlace(state, {payload}){
      return {...state, signPlace: payload}
    },
    //设置选择签退的人员
    setCurrentUserList(state, {payload}){
      const {currentUserList}=state;
      let has = false;
      let list = currentUserList;
      for (let i = 0; i < list.length; i++) {
        if (payload === list[i].id) {
          has = true;
          list.splice(i, 1);
          break
        }
      }
      if (!has) {
        list.push({id: payload});
      }
      return {...state, currentUserList: list}
    },
    //设置签退时间
    setSignTime(state, {payload}){
      return {...state, signTime: payload};
    },
    //初始化界面数据
    cleanData(state, {payload}){

      return {...state, signPlace: '',dialogText:''};
    },
      dialogShow(state,{payload}){
          return {...state,dialogText:payload}
      },
      getThisClean(state,{payload}){
          return {...state,
              taskName: '',//任务名称
              taskId: '',//任务id
              taskNum: '',//任务编号
              projectName: '',//工程名称
              projectNum: '',//工程编号
              signName: '',//签退人
              signTime: '',//签退时间
              signPlace: '',//签退地点
              taskPhotoList: [],//现场照片数组{photoPath:照片路径}
              taskSignNameList: [],//签名数组{signNamePath:照片路径}
              taskUserList: [],//人员数组{value:人员id,label:人员姓名}
              currentUserList: [],//选择签退的人员{id:人员id}
              dialogText:''
          }
      },
  },
}